﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FSY.Components.DataProvider;
using FSY.API.CacheManager;
using FSY.API.Utilities;
using FSY.Globals;
namespace FSY.Components.Entities
{
    public class Fsy_ThemeController
    {
        private static int InsertDeleteUpdate(Fsy_Theme theme, DataProviderAction action)
        {
            Fsy_Cache.RemoveCache(cacheThemeAll);
            return Fsy_DataProvider.Instance.Theme_AddDeleteUpdate(theme, action);
        }
        public static int Insert(Fsy_Theme theme)
        {
            return InsertDeleteUpdate(theme, DataProviderAction.Insert);
        }
        public static int Delete(int themeID)
        {
            Fsy_Theme theme = new Fsy_Theme();
            theme.ThemeID = Fsy_Null.SetNullInteger(themeID);
            return InsertDeleteUpdate(theme, DataProviderAction.Delete);
        }
        public static int Update(Fsy_Theme theme)
        {
            return InsertDeleteUpdate(theme, DataProviderAction.Update);
        }

        private static object objlock = new object();
        private static string cacheThemeAll = "ThemeAll";
        public static List<Fsy_Theme> GetAllTheme()
        {
            List<Fsy_Theme> lsThemes = new List<Fsy_Theme>();
            lock (objlock)
            {
                lsThemes = (List<Fsy_Theme>)Fsy_Cache.GetCache(cacheThemeAll);
                if (lsThemes == null)
                {
                    lsThemes = Fsy_DataProvider.Instance.Theme_GetListAll();
                    Fsy_Cache.SetCache(cacheThemeAll, lsThemes, Fsy_Config.SlidingExpiration());                   
                }
            }
            return lsThemes;
        }
        public static Fsy_Theme GetThemeIsDefault()
        {
            List<Fsy_Theme> ls = GetAllTheme();
            foreach (Fsy_Theme theme in ls)
            {
                if (theme.IsDefault == true)
                {
                    return theme;
                }
            }
            return null;
        }
        public static Fsy_Theme GetThemeByID(int themeID)
        {
            List<Fsy_Theme> ls = GetAllTheme();
            foreach (Fsy_Theme theme in ls)
            {
                if (theme.ThemeID == themeID)
                {
                    return theme;
                }
            }
            return null;
        }        
    }
}
